package com.easy.easy.Controller;

import com.easy.easy.Service.IAddressService;
import com.easy.easy.Service.IOrderAddressService;
import com.easy.easy.Service.IOrderinfoService;
import com.easy.easy.bean.Orderaddress;
import com.easy.easy.bean.Orderinfo;
import com.easy.easy.bean.User;
import com.easy.easy.bean.Useraddress;
import com.easy.easy.util.LayData;
import com.easy.easy.util.Page;
import com.easy.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("orderaddress")
public class OrderaddressController {
    @Autowired
    IOrderAddressService orderAddressService;

//    @RequestMapping("add")
//    @ResponseBody
//    public int add(Orderaddress orderaddress) {
//        return orderAddressService.add(orderaddress);
//    }

    @RequestMapping("add")
    @ResponseBody
    public LayData add(HttpSession session, Orderaddress orderaddress){
        System.out.println("进入add方法");

        User user=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);

        System.out.println("当前userid="+user.getUserid());
        orderaddress.setUserid(user.getUserid());
        System.out.println(orderaddress);
        orderAddressService.add(orderaddress);
        return LayData.success(200,"添加成功",orderaddress.getOrderid());
    }

    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text, HttpSession session) {
        System.out.println("进入cartlist方法");
        User user = (User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        List<Orderaddress> list = orderAddressService.getList(page, text, user.getUserid());
        int count = orderAddressService.getCount(text, user.getUserid());
        System.out.println(count);
        return new LayData(count,list);
    }

    @RequestMapping("getaddressbyuserid")
    @ResponseBody
    public LayData getAddressByUserID(HttpSession session) {
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid = loginUser.getUserid();
        List<Orderaddress> addresses = orderAddressService.getAddressByUserID(userid);
        return LayData.success(addresses);
    }
}
